<?php
class DBCon{
    protected $server;
    protected $user;
    protected $pwd;
    protected $db;
    protected $link;
    
    public function __construct($server,$user,$pwd,$db){
        $this->server=$server;
        $this->user=$user;
        $this->pwd=$pwd;
        $this->db=$db;
        
        $this->connect();
    }
    
    protected function connect(){
    }
    
    public function isConnected(){
        return $this->link!==FALSE;
    }
    
    public function exec($sql){
        
    }
    public function query($sql){
        $cur=$this->queryCursor($sql);
        return $cur->fetchAll();
    }
    public function queryOne($sql){
        $cur=$this->queryCursor($sql);
        return $cur->fetchOne();
    }
    
    /**
     *查询并返回一个Cursor类
     *@return DBCursor 查询并返回一个Cursor类
    */
    public function queryCursor($sql){
        
    }
}

class DBCursor{
    protected $res;
    protected $curData;
    public function __construct($resource){
        $this->res=$resource;
    }
    
    public function fetchOne(){
    }
    
    public function fetchAll(){
        $data=array();
        while($this->fetchOne()){
            $data[]=$this->curData;
        }
        return $data;
    }
    public function getCurrent(){
        return $this->curData;
    }
}

class DBMysqlCursor extends DBCursor{
    public function __construct($resource){
        parent::__construct($resource);
    }
    public function fetchOne(){
        $this->curData=mysql_fetch_assoc($this->res);
        return $this->curData;
    }
}

class DBMysql extends DBCon{
    public function __construct($server,$user,$pwd,$db){
        parent::__construct($server,$user,$pwd,$db);
        if($this->isConnected()){
            mysql_select_db($this->db,$this->link);
        }
    }
    protected function connect(){
        $this->link=mysql_connect($this->server,$this->user,$this->pwd);
    }
    
    public function exec($sql){
        mysql_query($sql,$this->link);
        return mysql_affected_rows($this->link);
    }
    
    public function insertID(){
        return mysql_insert_id($this->link);
    }
    public function queryCursor($sql){
        $resource=mysql_query($sql,$this->link);
        return new DBMysqlCursor($resource);
    }
}
